package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;

import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 新增预约信息
     * @param reservationDto
     */
    @Override
    public void add(ReservationDto reservationDto) {
        //获取线程中的userId
        Long userId =Long.parseLong(UserThreadLocal.getSubject());

        //把DTO的值拷贝给实体类
        //Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        Reservation reservation = new Reservation();
        BeanUtil.copyProperties(reservationDto, reservation);

        //补充缺少字段
        reservation.setCreateBy(userId);
        reservation.setCreateTime(LocalDateTime.now());
        reservation.setStatus(0);

        //完成新增
        customerReservationMapper.insert(reservation);
    }

    /**
     * 分页查询所有的预约数据
     * @param status
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public PageResponse<ReservationVo> selectPage(Integer status, Integer pageSize, Integer pageNum) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //完成分页查询
        Page<ReservationVo> page = customerReservationMapper.selectPage(status);
        //返回结果封装
        return PageResponse.of(page, ReservationVo.class);
    }

    /**
     * 取消预约 把预约状态更新为2
     * 统计该用户的取消剩余次数
     * @param id
     */
    @Override
    public void cancle(Long id) {
        //获取线程中的userId
        Long userID = Long.parseLong(UserThreadLocal.getSubject());

        //补充字段
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setUpdateBy(userID);
        reservation.setUpdateTime(LocalDateTime.now());
        reservation.setStatus(2);

        customerReservationMapper.update(reservation);

    }

    /**
     * 统计该用户所有在时间段里的status=2的预约数据
     * 当日0点LocalDateTime.now().withHour(0).withMinute(0).withSecond(0)
     * 当日24点LocalDateTime.now().withHour(23).withMinute(59).withSecond(59)
     * @param userId
     * @return
     */
    @Override
    public Integer getCancelledReservationCount(String userId) {
        Integer count =customerReservationMapper. getCancelledReservationCount(LocalDateTime.now().withHour(0).withMinute(0).withSecond(0),
                                                                                LocalDateTime.now().withHour(23).withMinute(59).withSecond(59),
                                                                                userId);
        return count;
    }

    /**
     * 统计每天不同预约时间段的预约表数量
     * 此时的时间作为开始时间到今天的结束时间作为结束时间
     * @param starTime
     * @return
     */
    @Override
    public List<TimeCountVo> countByTime(LocalDateTime starTime) {
        //定义结束时间
        LocalDateTime endTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        List<TimeCountVo> list =  customerReservationMapper.getRemainReservationCount(starTime,endTime);
        return list;
    }

    /**
     * 如果到了时间点还未完成（status=1）预约，则更新状态到过期（status =4）
     * @param id
     */
    @Override
    public void updateStatusToExpired(Long id) {
        //1.找到当前id对应的预约信息
        Reservation reservation =  customerReservationMapper.getById(id);
        //2.如果当前预约信息已经完成或者取消
        if(reservation.getStatus()==1 || reservation.getStatus()==2){
            return;
        }
        //3.拿到预约时间 判断当前时间是不是预约时间之后1一个小时 如果是则把状态更新为3
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime Time = reservation.getTime();//预约时间

        //4.判断预约时间是否落后于现在的时间1小时并且该预约事件为状态待完成 status == 0
        if(Time.isAfter(currentTime.minusHours(1)) && reservation.getStatus() == 0){
            reservation.setStatus(3);
            reservation.setUpdateTime(currentTime);
            customerReservationMapper.update(reservation);
        }
    }

    /**
     * 定时任务：获取到所有预约的Ids
     * @return
     */
    public List<Long> getAll() {
        return customerReservationMapper.getAllIds();
    }
}
